/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * 迭代实现
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal = function(root) {
    if(!root)return [];
    let res = [],
        cur = root,
        stack = [root];

    while(stack.length>0){

        cur = cur.left;
        while(cur){
            stack.push(cur);
            cur = cur.left;
        }

        cur = stack.pop();
        res.push(cur.val);

        cur = cur.right;

        while(!cur&&stack.length>0){
            cur = stack.pop();
            res.push(cur.val);
            cur = cur.right;
        }

        if(cur)stack.push(cur);
    }

    return res;
};